/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function validate(regex, input) {
    return regex.test(input);
}
function validateAndOutput(regex, input, errorToWrite, validToWrite, idToWrite) {
	var valid = (regex.test(input));
	validToWrite = '<span class="valid">' + validToWrite + '</span>';
	errorToWrite = '<span class="error">' + errorToWrite + '</span>';
	document.getElementById(idToWrite).innerHTML = valid ? validToWrite : errorToWrite;
	return valid;
 }
function validateFullname() {
        validateAndOutput(/^.{3,}$/, document.getElementById('fullname').value,'ERROR!','OK','fullname_info');
}
function validateUsername() {
	validateAndOutput(/^[\w\.]{5,}$/, document.getElementById('username').value,'ERROR!','OK','username_info');
}

function validatePassword() {
	validateAndOutput(/^.{6,}$/, document.getElementById('password').value,'ERROR!','OK','password_info');
}

function validateRePassword() {
	var pw1 = document.getElementById('password').value;
	var pw2 = document.getElementById('repassword').value;
	if(pw2 != pw1)	document.getElementById('repassword_info').innerHTML = '<span class="error">ERROR</span>';
	else			document.getElementById('repassword_info').innerHTML = '<span class="valid">OK</span>';
}

function validateLocation() {
	validateAndOutput(/^.{5,}$/, document.getElementById('location').value,'ERROR!','OK','location_info');
}

function validateBirthdate() {
	if(!validateAndOutput(/^\d{4}(-\d{2}){2}$/, document.getElementById('birthdate').value,'ERROR!','OK','birthdate_info')) return null;
	date = document.getElementById('birthdate').value.split('-');
	validDate = new Date(date[0], date[1], date[2]);
        
	valid = validDate.getFullYear() == date[0] && validDate.getMonth() == date[1] && validDate.getDate() == date[2];
	validateAndOutput(/^.$/, valid ? 'a' : 'asdf','ERROR!','OK','birthdate_info');
}

function validateAll() {
   var pw1 = document.getElementById('password').value;
   var pw2 = document.getElementById('repassword').value;

   date = document.getElementById('birthdate').value.split('-');
   validDate = new Date(date[0], date[1], date[2]);
   var isValidBirthDate = validate(/^\d{4}(-\d{2}){2}$/, document.getElementById('birthdate').value) && (validDate.getFullYear() == date[0] && validDate.getMonth() == date[1] && validDate.getDate() == date[2]);

   if (!(validate(/^[\w\.]{5,}$/, document.getElementById('username').value) && validate(/^.{6,}$/, document.getElementById('password').value) && (pw1==pw2) && validate(/^.{5,}$/, document.getElementById('location').value) && isValidBirthDate))
   {
        alert("Please fill the form correctly!");
        return false;
   }
   else
   {
       return true;
   }
}

function validateProfpict() {
	if (document.getElementById('profpict')!=null)
            validateAndOutput(/\.(jpg||pjpeg|jpeg|bmp|png)$/, document.getElementById('profpict').value,'ERROR!','OK','profpict_info');
}

function validateAllEditProfile() {
   var pw1 = document.getElementById('password').value;
   var pw2 = document.getElementById('repassword').value;

   date = document.getElementById('birthdate').value.split('-');
   validDate = new Date(date[0], date[1], date[2]);
   var isValidBirthDate = validate(/^\d{4}(-\d{2}){2}$/, document.getElementById('birthdate').value) && (validDate.getFullYear() == date[0] && validDate.getMonth() == date[1] && validDate.getDate() == date[2]);

   if (!(validate(/^.{6,}$/, document.getElementById('password').value) && (pw1==pw2) && validate(/^.{5,}$/, document.getElementById('location').value) && isValidBirthDate))
   {
        alert("Please fill the form correctly!")
        return false;
   }
   else
   {
    return true;
   }
   //return(validate(/\.(jpg||pjpeg|jpeg|bmp|png)$/, document.getElementById('profpict').value),validate(/^.{6,}$/, document.getElementById('password').value) && (pw1==pw2) && validate(/^.{5,}$/, document.getElementById('location').value) && isValidBirthDate);
}

function validatePhotoUpload() {
	validateAndOutput(/\.(jpg||pjpeg|jpeg|bmp|png)$/, document.getElementById('file').value,'ERROR!','OK','file_info');
}

function validateTitle() {
    validateAndOutput(/^.{5,50}$/, document.getElementById('title').value,'ERROR!','OK','title_info');
}

function validateDescription() {
    validateAndOutput(/^.{5,}$/, document.getElementById('description').value,'ERROR!','OK','description_info');
}

function validateDate() {
	if(!validateAndOutput(/^\d{4}(-\d{2}){2}$/, document.getElementById('date').value,'ERROR!','OK','date_info')) return null;
	date = document.getElementById('date').value.split('-');
	validDate = new Date(date[0], date[1], date[2]);

	valid = validDate.getFullYear() == date[0] && validDate.getMonth() == date[1] && validDate.getDate() == date[2];
	validateAndOutput(/^.$/, valid ? 'a' : 'asdf','ERROR!','OK','date_info');
}

function validateAllUploadPhoto() {
   date = document.getElementById('date').value.split('-');
   validDate = new Date(date[0], date[1], date[2]);
   var isValidDate = validate(/^\d{4}(-\d{2}){2}$/, document.getElementById('date').value) && (validDate.getFullYear() == date[0] && validDate.getMonth() == date[1] && validDate.getDate() == date[2]);

   if (!(validate(/\.(jpg||pjpeg|jpeg|bmp|png)$/, document.getElementById('file').value) && validate(/^.{5,}$/, document.getElementById('description').value) && validate(/^.{5,50}$/, document.getElementById('title').value) && isValidDate))
   {
        alert("Please fill the form correctly!")
        return false;
   }
   else
   {
    return true;
   }
}

function validateSetting() {
    var pw1 = document.getElementById('password').value;
   var pw2 = document.getElementById('repassword').value;

   return (validate(/^.{6,}$/, document.getElementById('password').value) && (pw1==pw2));
}